package com.iuie.ns.system.service.setting.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.iuie.ns.frame.exception.ServiceException;
import com.iuie.ns.frame.persistence.dao.IGenericDao.MatchType;
import com.iuie.ns.frame.utils.ListUtils;
import com.iuie.ns.frame.utils.StringUtils;
import com.iuie.ns.frame.web.dataset.vo.Page;
import com.iuie.ns.system.mapper.setting.SysUserSettingTemplateMapper;
import com.iuie.ns.system.service.resource.ISysResourceService;
import com.iuie.ns.system.service.setting.ISysUserSettingService;
import com.iuie.ns.system.service.setting.ISysUserSettingTemplateService;
import com.iuie.ns.system.vo.resource.SysResourceVO;

@Service("userSettingTemplateService")
public class SysUserSettingTemplateServiceImpl implements ISysUserSettingTemplateService {
	
	public static final String RES_TYPE = "userSetting";
	
	@Resource
	private ISysResourceService resourceService;
	
	@Resource
	private SysUserSettingTemplateMapper userSettingTemplateMapper;
	
	@Resource
	private ISysUserSettingService userSettingService;

	@Override
	public List<SysResourceVO> findTemplateByPage(SysResourceVO condition, Page page) {
		if (condition == null) {
			condition = new SysResourceVO();
		}
		condition.setResType(RES_TYPE);
		Map<String, MatchType> matchCondition = new HashMap<String, MatchType>();
		matchCondition.put("resName", MatchType.MATCH_LIKE);
		
		List<SysResourceVO> list = resourceService.findByPage(condition, page, matchCondition);
		page.loadData(list);
		return list;
	}
	
	@Override
	public void addOneTemplate(SysResourceVO item) {
		if (item != null && StringUtils.isBlank(item.getId())) {
			item.setResId(StringUtils.uuid());
			item.setResType(RES_TYPE);
		}
		// ext1和ext2不能为空，且不能出现重复
		if (StringUtils.isBlank(item.getResExt1())) {
			throw new ServiceException("设置KEY不能为空");
		}
		if (StringUtils.isBlank(item.getResExt2())) {
			throw new ServiceException("设置的值类型不能为空");
		}
		SysResourceVO old = this.findOneBySettingKey(item.getResExt1());
		if (old != null) {
			throw new ServiceException("新增用户设置模板失败，已存在settingKey=" + item.getResExt1());
		}
		resourceService.addOne(item);
	}

	@Override
	public void updateOneTemplate(SysResourceVO item, String resId) {
		if (item != null) {
			item.setResId(resId);
			item.setResType(RES_TYPE);
		}
		resourceService.updateOne(item);
	}

	@Override
	public void deleteTemplate(String resId) {
		// 1.删除用户设置及其值
		SysResourceVO template = resourceService.findById(resId);
		if (template == null) {
			throw new ServiceException("删除用户设置模板失败，未查询到用户设置模板，模板ID： " + resId + "！");
		}
		userSettingService.deleteUserSettingsBySettingKey(template.getResExt1(), template.getResExt2());
		
		// 2.删除用户设置模板与角色的关联
		this.deleteUserSettingWithRoleRelationByResId(resId);
		
		resourceService.deleteById(resId);
	}

	@Override
	public SysResourceVO findById(String resId) {
		SysResourceVO item = resourceService.findById(resId);
		return item;
	}

	@Override
	public List<SysResourceVO> findAll() {
		// 此处仅查询用户设置模板
		SysResourceVO condition = new SysResourceVO();
		condition.setEnabled(1);
		condition.setResType(RES_TYPE);
		List<SysResourceVO> list = resourceService.findAll(condition);
		return list;
	}
	
	@Override
	public List<SysResourceVO> findTemplateByRoleId(String roleId) {
		return userSettingTemplateMapper.findTemplateByRoleId(roleId);
	}

	@Override
	public void saveUserSettingWithRoleRelation(String roleId, String[] resIdArr) {
		// 1.删除之前的
		this.deleteUserSettingWithRoleRelation(roleId);
		
		// 2.保存最新的
		if (resIdArr == null || resIdArr.length <= 0 ) {
			return ;
		}
		userSettingTemplateMapper.saveUserSettingWithRoleRelation(roleId, resIdArr);
	}
	
	@Override
	public Integer deleteUserSettingWithRoleRelation(String roleId) {
		Integer count = userSettingTemplateMapper.deleteUserSettingWithRoleRelation(roleId);
		return count;
	}
	
	@Override
	public Integer deleteUserSettingWithRoleRelationByResId(String resId) {
		Integer count = userSettingTemplateMapper.deleteUserSettingWithRoleRelationByResId(resId);
		return count;
	}
	
	@Override
	public String findStringOfUserSettingTemplatesByRoleId(String roleId) {
		return userSettingTemplateMapper.findStringOfUserSettingTemplatesByRoleId(roleId);
	}

	@Override
	public SysResourceVO findOneBySettingKey(String settingKey) {
		SysResourceVO condition = new SysResourceVO();
		condition.setResExt1(settingKey);
		List<SysResourceVO> list = resourceService.findAll(condition);
		if (ListUtils.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}
}
